package mapEditor.view.mapEdit
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	
	import iso.IsoDisplayObject;
	import iso.IsoGrid;
	import iso.IsoPoint;
	import iso.IsoUtils;
	
	import keyboard.KeyComboCell;
	import keyboard.KeyEvent;
	import keyboard.KeyboardFactory;
	
	import map.findPath.FindPath;
	import map.findPath.FindPathNode;
	
	import mapEditor.MapEditorManager;
	import mapEditor.data.ConfigTemplateInfo;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.vo.MapTerrainTileVO;
	import mapEditor.vo.ToolType;
	
	import utils.ColorMatrix;
	import utils.DisplayUtil;

	public class MapTerrainEditView extends Sprite
	{
		/**
		 *地图网格层 
		 */		
		private var _mapGridLayer:IsoGrid;
		private var _overMapTerrainTileView:MapTerrainTileView;
		/**
		 *编辑区大小 
		 */
		private var _areaRec:Rectangle;
		private var _terrainTileList:Vector.<MapTerrainTileView>=new Vector.<MapTerrainTileView>();
		private var _terrainTileDic:Dictionary=new Dictionary(true);
		private var _selectTerrainTileList:Vector.<MapTerrainTileView>=new Vector.<MapTerrainTileView>();
		private var _dragRec:Rectangle=new Rectangle();
		private var _dragRecLayer:IsoDisplayObject;
		/**
		 *按alt键后记录的对地形块拉伸的起始坐标 
		 */		
		private var _scaleStartPoint:Point=new Point();
		private var _tempLayer:Sprite;
		
		public function MapTerrainEditView(areaRec:Rectangle)
		{
			_areaRec=areaRec;
			setView();
			addEvent();
		}
		
		private var _txtPathfinding:TextField;
		private function setView():void
		{
			drawEditArea();
			_mapGridLayer=new IsoGrid(2, ConfigTemplateInfo.instance.mapVO.tileSize, ConfigTemplateInfo.instance.mapVO.mapRow, ConfigTemplateInfo.instance.mapVO.mapCols, true);
			_mapGridLayer.x=(_areaRec.width-_mapGridLayer.width)/2+_mapGridLayer.width/2;
			addChild(_mapGridLayer);
			resetTerrainTileList();
			
			_dragRecLayer=new IsoDisplayObject();
			_dragRecLayer.x=(_areaRec.width-_mapGridLayer.width)/2+_mapGridLayer.width/2;
			addChild(_dragRecLayer);
			
			_txtPathfinding=new TextField();
			addChild(_txtPathfinding);
			
//			_tempLayer=new Sprite();
//			addChild(_tempLayer);
		}
		
		private function addEvent():void
		{
			addEventListener(MouseEvent.CLICK, __onThisMouseEvent);
			addEventListener(MouseEvent.MOUSE_MOVE, __onThisMouseEvent);
			addEventListener(MouseEvent.MOUSE_DOWN, __onThisMouseEvent);
			addEventListener(MouseEvent.MOUSE_OUT, __onThisMouseEvent);
			ConfigTemplateInfo.instance.mapVO.addEventListener(Event.CHANGE, __onMapVOChange);
			MapEditorManager.instance.mapEditorModel.addEventListener(MapEditorEvent.TOOL_TYPE_CHANGE, __onToolTypeChange);
			KeyboardFactory.Instance.addKeyCombo("terrainEdit");
			KeyboardFactory.Instance.addEventListener(KeyEvent.COMBO_ALL_KEY_DOWN, __onComboAllKeyDown);
			KeyboardFactory.Instance.addEventListener(KeyEvent.KEY_UP, __onKeyUp);
			KeyboardFactory.Instance.createCombosKey("terrainEdit", [Keyboard.ALTERNATE], Keyboard.ALTERNATE);
			
			MapEditorManager.instance.mapEditorModel.addEventListener(MapEditorEvent.FIND_PATH_NODES_CHANGE, __onFindPathNodesChange);
		}
		
		private function removeEvent():void
		{
			removeEventListener(MouseEvent.CLICK, __onThisMouseEvent);
			removeEventListener(MouseEvent.MOUSE_MOVE, __onThisMouseEvent);
			removeEventListener(MouseEvent.MOUSE_DOWN, __onThisMouseEvent);
			removeEventListener(MouseEvent.MOUSE_OUT, __onThisMouseEvent);
			removeEventListener(MouseEvent.MOUSE_UP, __onThisMouseEvent);
			ConfigTemplateInfo.instance.mapVO.removeEventListener(Event.CHANGE, __onMapVOChange);
			MapEditorManager.instance.mapEditorModel.removeEventListener(MapEditorEvent.TOOL_TYPE_CHANGE, __onToolTypeChange);
			KeyboardFactory.Instance.removeEventListener(KeyEvent.COMBO_ALL_KEY_DOWN, __onComboAllKeyDown);
			KeyboardFactory.Instance.removeEventListener(KeyEvent.KEY_UP, __onKeyUp);
			
			MapEditorManager.instance.mapEditorModel.removeEventListener(MapEditorEvent.FIND_PATH_NODES_CHANGE, __onFindPathNodesChange);
		}
		
		private function __onComboAllKeyDown(evt:KeyEvent):void
		{
			var keyComboCell:KeyComboCell=evt.data as KeyComboCell;
			if(keyComboCell.type!="terrainEdit")return;
			
			switch(keyComboCell.data)
			{
				case Keyboard.ALTERNATE:
					_scaleStartPoint.x=mouseX;
					_scaleStartPoint.y=mouseY;
					clearOverMapTerrainTileView();
					break;
			}
		}
		
		private function __onKeyUp(evt:KeyEvent):void
		{
			_scaleStartPoint.x=0;
			_scaleStartPoint.y=0;
		}
		
		private function __onFindPathNodesChange(evt:MapEditorEvent):void
		{
			var n:int=0;
			for each(var node:FindPathNode in MapEditorManager.instance.mapEditorModel.findPathNodes)
			{
				for(var w:int=0;w<node.width;w++)
				{
					for(var h:int=0;h<node.height;h++)
					{
						var mapTerrainTileView:MapTerrainTileView=_terrainTileDic[node.x+w][node.y+h];
						mapTerrainTileView.txtTest.text=n.toString();
					}
				}
				n++;
			}
		}
		
		private var _startNode:MapTerrainTileVO;
		private var _startPoint:Point;
		private var _endPoint:Point;
		private function __onThisMouseEvent(evt:MouseEvent):void
		{
			switch(evt.type)
			{
				case MouseEvent.CLICK:
					
					
					var pt:Point=new Point(_mapGridLayer.mouseX, _mapGridLayer.mouseY);
					var ipt:IsoPoint=IsoUtils.screenToSpace(pt);
					var row:int=Math.floor(ipt.z/ConfigTemplateInfo.instance.mapVO.tileSize);
					var cols:int=Math.floor(ipt.x/ConfigTemplateInfo.instance.mapVO.tileSize);
					
					if(evt.ctrlKey)
					{
						if(_startPoint==null)
						{
							_startPoint=new Point();
							_startPoint.x=ipt.x;
							_startPoint.y=ipt.z;
							for each(var mtv:MapTerrainTileView in _terrainTileList)
							{
								mtv.filters=[];
							}
						}
						else
						{
							_endPoint=new Point();
							_endPoint.x=ipt.x;
							_endPoint.y=ipt.z;
							var tim1:Number=getTimer();
							var findPath:FindPath=new FindPath(MapEditorManager.instance.mapEditorModel.findPathNodes, ConfigTemplateInfo.instance.mapVO.tileSize, ConfigTemplateInfo.instance.mapVO.walkableHeight, ConfigTemplateInfo.instance.mapVO.walkableBottom);
							if(findPath.findPath(_startPoint, _endPoint))
							{
								var tim2:Number=getTimer();
								_txtPathfinding.text=String(tim2-tim1);
								for each(var findPathNode:FindPathNode in findPath.findPaths)
								{
									for(var w:int=0;w<findPathNode.width;w++)
									{
										for(var h:int=0;h<findPathNode.height;h++)
										{
											var mapTerrainTileView:MapTerrainTileView=_terrainTileDic[findPathNode.x+w][findPathNode.y+h];
											var colorMatrixFilter:ColorMatrixFilter=new ColorMatrixFilter();
											var colorMatrix:ColorMatrix=new ColorMatrix();
											colorMatrix.adjustBrightness(-200);
											colorMatrixFilter.matrix=colorMatrix;
											mapTerrainTileView.filters=[colorMatrixFilter];
										}
									}
								}
								
								_dragRecLayer.graphics.clear();
								_dragRecLayer.graphics.lineStyle(1, 0xff0000);
								var p:Point=IsoUtils.spaceToScreen(new IsoPoint(_startPoint.x, 0, _startPoint.y));
								_dragRecLayer.graphics.moveTo(p.x, p.y);
								for each(var point:Point in findPath.path)
								{
									p=IsoUtils.spaceToScreen(new IsoPoint(point.x, 0, point.y));
									_dragRecLayer.graphics.lineTo(p.x, p.y);
								}
								_dragRecLayer.graphics.endFill();
							}
							_startPoint=null;
							_endPoint=null;
						}
						
//						if(!_startNode)
//						{
//							_startNode=_terrainTileDic[cols][row].mapTerrainTileVO;
//							for each(var mtv:MapTerrainTileView in _terrainTileList)
//							{
//								mtv.filters=[];
//							}
//						}
//						else
//						{
//							var tim1:Number=getTimer();
//							var mapPathfinding:MapPathfinding=new MapPathfinding();
//							if(mapPathfinding.findPath(_mapNodes, ConfigTemplateInfo.instance.mapVO.mapCols, ConfigTemplateInfo.instance.mapVO.mapRow, _startNode, _terrainTileDic[cols][row].mapTerrainTileVO))
//							{
//								var tim2:Number=getTimer();
//								_txtPathfinding.text=String(tim2-tim1);
//								
//								for each(var mapTerrainTileVO:MapTerrainTileVO in mapPathfinding.visited)
//								{
//									var mapTerrainTileView:MapTerrainTileView=_terrainTileDic[mapTerrainTileVO.cols][mapTerrainTileVO.row];
//									var colorMatrixFilter:ColorMatrixFilter=new ColorMatrixFilter();
//									var colorMatrix:ColorMatrix=new ColorMatrix();
//									colorMatrix.adjustBrightness(-200);
//									colorMatrixFilter.matrix=colorMatrix;
//									mapTerrainTileView.filters=[colorMatrixFilter];
//								}
//								
//								for each(mapTerrainTileVO in mapPathfinding.path)
//								{
//									mapTerrainTileView=_terrainTileDic[mapTerrainTileVO.cols][mapTerrainTileVO.row];
//									colorMatrixFilter=new ColorMatrixFilter();
//									colorMatrix=new ColorMatrix();
//									colorMatrix.adjustColor(100,100,100,100);
//									colorMatrixFilter.matrix=colorMatrix;
//									mapTerrainTileView.filters=[colorMatrixFilter];
//								}
//							}
//							_startNode=null;
//						}
					}
					break;
				case MouseEvent.MOUSE_DOWN:
					addEventListener(MouseEvent.MOUSE_UP, __onThisMouseEvent);
					switch(MapEditorManager.instance.mapEditorModel.toolType)
					{
						case ToolType.TOOL_GRID_SELECT://格子选择工具
							_dragRec.x=_mapGridLayer.mouseX;
							_dragRec.y=_mapGridLayer.mouseY;
							break;
					}
					break;
				case MouseEvent.MOUSE_MOVE:
					switch(MapEditorManager.instance.mapEditorModel.toolType)
					{
						case ToolType.TOOL_GRID_SELECT://选择工具
							if(evt.buttonDown && !evt.altKey)
							{
								_dragRec.width=_mapGridLayer.mouseX-_dragRec.x;
								_dragRec.height=_mapGridLayer.mouseY-_dragRec.y;
//								dragRecSelect();
							}
							else if(evt.buttonDown && evt.altKey)
							{
								setSelectTerrainTileAddHeight(_scaleStartPoint.y-mouseY);
								_scaleStartPoint.x=mouseX;
								_scaleStartPoint.y=mouseY;
							}
							break;
					}
					break;
				case MouseEvent.MOUSE_UP:
					removeEventListener(MouseEvent.MOUSE_UP, __onThisMouseEvent);
//					dragRecSelect();
					break;
				case MouseEvent.MOUSE_OUT:
					
					break;
			}
			__onGridLayerMouseEvent(evt);
		}
		
		/**
		 * 矩形区域内的项选择处理
		 * 
		 */		
		private function dragRecSelect():void
		{
			_dragRec.width=mouseX-_dragRec.x;
			_dragRec.height=mouseY-_dragRec.y;
//			if(_dragRec.width<0)
//			{
//				_dragRec.x=_dragRec.x+_dragRec.width;
//				_dragRec.width=Math.abs(_dragRec.width);
//			}
//			if(_dragRec.height<0)
//			{
//				_dragRec.y=_dragRec.y+_dragRec.height;
//				_dragRec.height=Math.abs(_dragRec.height);
//			}
			clearSelectTerrainTile();
			for each(var mapTerrainTileView:MapTerrainTileView in _terrainTileList)
			{
				var rect:Rectangle=new Rectangle();
				rect.x=mapTerrainTileView.point.x;
				rect.y=mapTerrainTileView.point.y;
				rect.x+=_mapGridLayer.x;
				rect.y+=_mapGridLayer.y;
				rect.width=mapTerrainTileView.size;
				rect.height=mapTerrainTileView.size;
				if(_dragRec.containsRect(rect))
				{
					mapTerrainTileView.mapTerrainTileVO.selected=true;
					_selectTerrainTileList.push(mapTerrainTileView);
				}
			}
			_dragRec.setEmpty();
			setSelectTerrainTile();
			if(_selectTerrainTileList.length==1)
			{
				MapEditorManager.instance.selectTerrainTile(_selectTerrainTileList[0].mapTerrainTileVO);
			}
			else
			{
				MapEditorManager.instance.selectTerrainTile(null);
			}
		}
		
//		private var testStartPoint:Point=new Point();
//		private function drawDragRec():void
//		{
//			_dragRecLayer.graphics.clear();
//			_dragRecLayer.graphics.lineStyle(1, 0x000000);
//
//			
////			var pt:Point = IsoUtils.spaceToScreen(new IsoPoint(testStartPoint.x, 0, testStartPoint.y));
////			_dragRecLayer.graphics.moveTo(pt.x, pt.y);
////			pt = IsoUtils.spaceToScreen(new IsoPoint(testEndPoint.x, 0, testEndPoint.y));
////			_dragRecLayer.graphics.lineTo(pt.x, pt.y);
//			
//			
//			
//			var pt:Point = IsoUtils.spaceToScreen(new IsoPoint(_dragRec.x, 0, _dragRec.y));
//			_dragRecLayer.graphics.moveTo(pt.x, pt.y);
//			pt = IsoUtils.spaceToScreen(new IsoPoint(_dragRec.x+_dragRec.width, 0, _dragRec.y));
//			_dragRecLayer.graphics.lineTo(pt.x, pt.y);
////			pt = IsoUtils.spaceToScreen(new IsoPoint(_dragRec.x+_dragRec.width, 0, _dragRec.y+_dragRec.height));
////			_dragRecLayer.graphics.lineTo(pt.x, pt.y);
////			pt = IsoUtils.spaceToScreen(new IsoPoint(_dragRec.x, 0, _dragRec.y+_dragRec.height));
////			_dragRecLayer.graphics.lineTo(pt.x, pt.y);
////			pt = IsoUtils.spaceToScreen(new IsoPoint(_dragRec.x, 0, _dragRec.y));
////			_dragRecLayer.graphics.lineTo(pt.x, pt.y);
//			
//			
//			
//			
//			
//			
//			
//			
//			
//			
//			
//			
////			var pt1:Point=new Point(_dragRec.x,_dragRec.y);
////			var ipt1:IsoPoint=IsoUtils.screenToSpace(pt1);
////			var pt2:Point=new Point(_dragRec.x+_dragRec.width,_dragRec.y);
////			var ipt2:IsoPoint=IsoUtils.screenToSpace(pt2);
////			var pt3:Point=new Point(_dragRec.x+_dragRec.width,_dragRec.y+_dragRec.height);
////			var ipt3:IsoPoint=IsoUtils.screenToSpace(pt3);
////			var pt4:Point=new Point(_dragRec.x,_dragRec.y+_dragRec.height);
////			var ipt4:IsoPoint=IsoUtils.screenToSpace(pt4);
//			
////			graphics.moveTo(-_dragRec.width, _dragRec.height/2);
////			graphics.lineTo(0, 0);
////			graphics.lineTo(_dragRec.width, _dragRec.height/2);
////			graphics.lineTo(0, _dragRec.width);
////			graphics.lineTo(-_dragRec.width, _dragRec.height/2);
//			
////			_dragRecLayer.graphics.moveTo(ipt1.x, ipt1.z);
////			_dragRecLayer.graphics.lineTo(ipt2.x, ipt2.z);
////			_dragRecLayer.graphics.lineTo(ipt3.x, ipt3.z);
////			_dragRecLayer.graphics.lineTo(ipt4.x, ipt4.z);
////			_dragRecLayer.graphics.lineTo(ipt1.x, ipt1.z);
////			_dragRecLayer.graphics.drawRect(_dragRec.x,_dragRec.y,_dragRec.width, _dragRec.height);
////			_dragRecLayer.rotation=45;
//			
////			var pt:Point = IsoUtils.spaceToScreen(new IsoPoint(0, 0));
////			_dragRecLayer.graphics.moveTo(pt.x, pt.y);
////			pt = IsoUtils.spaceToScreen(new IsoPoint(_dragRec.width, 0));
////			_dragRecLayer.graphics.lineTo(pt.x, pt.y);
////			pt = IsoUtils.spaceToScreen(new IsoPoint(_dragRec.width, 0, _dragRec.height));
////			_dragRecLayer.graphics.lineTo(pt.x, pt.y);
////			pt = IsoUtils.spaceToScreen(new IsoPoint(0, 0, _dragRec.height));
////			_dragRecLayer.graphics.lineTo(pt.x, pt.y);
////			pt = IsoUtils.spaceToScreen(new IsoPoint(0, 0));
////			_dragRecLayer.graphics.lineTo(pt.x, pt.y);
////			_dragRecLayer.x=_dragRec.x;
////			_dragRecLayer.y=_dragRec.y;
//			_dragRecLayer.graphics.endFill();
//			
//		}
		
		private function cereateAllTerrainTile():void
		{
			MapEditorManager.instance.mapEditorModel.clearMapNodes();
			for(var c:int=0;c<ConfigTemplateInfo.instance.mapVO.mapCols;c++)
			{
				for(var r:int=0;r<ConfigTemplateInfo.instance.mapVO.mapRow;r++)
				{
					if(_terrainTileDic[c]==null || _terrainTileDic[c][r]==null)
					{
						var mapTerrainTileVO:MapTerrainTileVO=new MapTerrainTileVO();
						mapTerrainTileVO.x=c;
						mapTerrainTileVO.y=r;
						
						var mapTerrainTileView:MapTerrainTileView=new MapTerrainTileView(mapTerrainTileVO);
						_terrainTileList.push(mapTerrainTileView);
						
						if(_terrainTileDic[mapTerrainTileView.mapTerrainTileVO.x]==null)_terrainTileDic[mapTerrainTileView.mapTerrainTileVO.x]=new Dictionary(true);
						_terrainTileDic[mapTerrainTileView.mapTerrainTileVO.x][mapTerrainTileView.mapTerrainTileVO.y]=mapTerrainTileView;
					}
				}
			}
			
			for(c=0;c<ConfigTemplateInfo.instance.mapVO.mapCols;c++)
			{
				if(MapEditorManager.instance.mapEditorModel.mapNodes[c]==null)MapEditorManager.instance.mapEditorModel.mapNodes[c]=[];
				for(r=0;r<ConfigTemplateInfo.instance.mapVO.mapRow;r++)
				{
					mapTerrainTileView=_terrainTileDic[c][r];
					MapEditorManager.instance.mapEditorModel.mapNodes[c][r] = mapTerrainTileView.mapTerrainTileVO;
				}
			}
			
//			for(var i:int = 0; i < _numCols; i++)
//			{
//				_nodes[i] = new Array();
//				for(var j:int = 0; j < _numRows; j++)
//				{
//					_nodes[i][j] = new Node(i, j);
//				}
//			}
		}
		
		private function __onToolTypeChange(evt:MapEditorEvent):void
		{
			switch(MapEditorManager.instance.mapEditorModel.toolType)
			{
				case ToolType.TOOL_GRID_SELECT:
					break;
			}
		}
		
		private function __onGridLayerMouseEvent(evt:MouseEvent):void
		{
			var pt:Point=new Point(_mapGridLayer.mouseX, _mapGridLayer.mouseY);
			var ipt:IsoPoint=IsoUtils.screenToSpace(pt);
			var row:int=Math.floor(ipt.z/ConfigTemplateInfo.instance.mapVO.tileSize);
			var cols:int=Math.floor(ipt.x/ConfigTemplateInfo.instance.mapVO.tileSize);
			
			switch(evt.type)
			{
				case MouseEvent.MOUSE_DOWN:
					if(MapEditorManager.instance.mapEditorModel.toolType==ToolType.TOOL_GRID_SELECT)
					{
						if(row<0 || row>=ConfigTemplateInfo.instance.mapVO.mapRow || cols<0 || cols>=ConfigTemplateInfo.instance.mapVO.mapCols)
						{
							clearSelectTerrainTile();
						}
					}
					break;
				case MouseEvent.CLICK:
					if(MapEditorManager.instance.mapEditorModel.toolType==ToolType.TOOL_GRID_SELECT)
					{
						if(row<0 || row>=ConfigTemplateInfo.instance.mapVO.mapRow || cols<0 || cols>=ConfigTemplateInfo.instance.mapVO.mapCols)return;
						
						if(!evt.ctrlKey)
						{
							clearSelectTerrainTile();
						}
						
						var mapTerrainTileView:MapTerrainTileView=_terrainTileDic[cols][row];
						addSelectTerrainTile(mapTerrainTileView);
						setSelectTerrainTile();
						
						if(_selectTerrainTileList.length==1)
						{
							MapEditorManager.instance.selectTerrainTile(_selectTerrainTileList[0].mapTerrainTileVO);
						}
						else
						{
							MapEditorManager.instance.selectTerrainTile(null);
						}	
					}
					break;
				case MouseEvent.MOUSE_MOVE:
					if(MapEditorManager.instance.mapEditorModel.toolType==ToolType.TOOL_GRID_SELECT)
					{
						if(!evt.altKey)
						{
							if(_overMapTerrainTileView && _selectTerrainTileList.indexOf(_overMapTerrainTileView)==-1)
							{
								_overMapTerrainTileView.filters=[];
							}
							if(row<0 || row>=ConfigTemplateInfo.instance.mapVO.mapRow || cols<0 || cols>=ConfigTemplateInfo.instance.mapVO.mapCols)return;
							
							_overMapTerrainTileView=_terrainTileDic[cols][row];
							filterTerrainTileView(_overMapTerrainTileView);
						}
					}
					break;
				case MouseEvent.MOUSE_OUT:
					clearOverMapTerrainTileView();
					break;
			}
		}
		
		private function clearOverMapTerrainTileView():void
		{
			if(_overMapTerrainTileView && _selectTerrainTileList.indexOf(_overMapTerrainTileView)==-1)
			{
				_overMapTerrainTileView.filters=[];
			}
		}
		
		private function filterTerrainTileView(mapTerrainTileView:MapTerrainTileView):void
		{
			var colorMatrixFilter:ColorMatrixFilter=new ColorMatrixFilter();
			var colorMatrix:ColorMatrix=new ColorMatrix();
			colorMatrix.adjustBrightness(-50);
			colorMatrixFilter.matrix=colorMatrix;
			mapTerrainTileView.filters=[colorMatrixFilter];
		}
		
		/**
		 *增加一个选择项 
		 * @param mapTerrainTileView
		 * 
		 */		
		private function addSelectTerrainTile(mapTerrainTileView:MapTerrainTileView):void
		{
			if(_selectTerrainTileList.indexOf(mapTerrainTileView)!=-1)return;
			_selectTerrainTileList.push(mapTerrainTileView);
		}
		
		/**
		 *标示所有当前已选的地形块 
		 * 
		 */		
		private function setSelectTerrainTile():void
		{
			if(_selectTerrainTileList.length<=0)return;
			for each(var mapTerrainTileView:MapTerrainTileView in _selectTerrainTileList)
			{
				mapTerrainTileView.mapTerrainTileVO.selected=true;
				mapTerrainTileView.mapTerrainTileVO.change();
			}
		}
		
		/**
		 *设置所有当前已选的地形块的要增加的高度
		 * 
		 */		
		private function setSelectTerrainTileAddHeight(addHeight:Number):void
		{
			if(_selectTerrainTileList.length<=0)return;
			for each(var mapTerrainTileView:MapTerrainTileView in _selectTerrainTileList)
			{
				mapTerrainTileView.mapTerrainTileVO.height+=addHeight;
				mapTerrainTileView.mapTerrainTileVO.change();
			}
		}
		
		/**
		 *清除所有当前已选的地形块 
		 * 
		 */		
		private function clearSelectTerrainTile():void
		{
			while(_selectTerrainTileList.length>0)
			{
				var mapTerrainTileView:MapTerrainTileView=_selectTerrainTileList.shift();
				mapTerrainTileView.mapTerrainTileVO.selected=false;
				mapTerrainTileView.mapTerrainTileVO.change();
			}
		}
		
		private function __onMapVOChange(evt:Event):void
		{
			_mapGridLayer.rowSize=ConfigTemplateInfo.instance.mapVO.mapRow;
			_mapGridLayer.colsSize=ConfigTemplateInfo.instance.mapVO.mapCols;
			_mapGridLayer.cellSize=ConfigTemplateInfo.instance.mapVO.tileSize;
			_mapGridLayer.update();
		}
		
		private function resetTerrainTileList():void
		{
			clearTerrainTileList();
			for each(var mapTerrainTileVO:MapTerrainTileVO in ConfigTemplateInfo.instance.mapVO.terrainTileList)
			{
				var mapTerrainTileView:MapTerrainTileView=new MapTerrainTileView(mapTerrainTileVO);
				_terrainTileList.push(mapTerrainTileView);
				
				if(_terrainTileDic[mapTerrainTileView.mapTerrainTileVO.x]==null)_terrainTileDic[mapTerrainTileView.mapTerrainTileVO.x]=new Dictionary(true);
				_terrainTileDic[mapTerrainTileView.mapTerrainTileVO.x][mapTerrainTileView.mapTerrainTileVO.y]=mapTerrainTileView;
				
				if(MapEditorManager.instance.mapEditorModel.mapNodes[mapTerrainTileView.mapTerrainTileVO.x]==null)MapEditorManager.instance.mapEditorModel.mapNodes[mapTerrainTileView.mapTerrainTileVO.x]=[];
				MapEditorManager.instance.mapEditorModel.mapNodes[mapTerrainTileView.mapTerrainTileVO.x][mapTerrainTileView.mapTerrainTileVO.y] = mapTerrainTileVO;
			}
			cereateAllTerrainTile();
			_terrainTileList.sort(sortOnDepth);
			for each(mapTerrainTileView in _terrainTileList)
			{
				mapTerrainTileView.mouseEnabled=false;
				_mapGridLayer.addChild(mapTerrainTileView);
			}
		}
		
		private function sortOnDepth(a:*, b:*):Number
		{
			var aDepth:Number = a.depth;
			var bDepth:Number = b.depth;
			
			if(bDepth > aDepth) {
				return -1;
			} else if(bDepth < aDepth) {
				return 1;
			} else  {
				return 0;
			}
		}
		
		private function clearTerrainTileList():void
		{
			while(_terrainTileList.length<0)
			{
				var mapTerrainTileView:MapTerrainTileView=_terrainTileList.shift();
				DisplayUtil.dispose(mapTerrainTileView)
				mapTerrainTileView=null;
			}
		}
		
		public function resize():void
		{
			_mapGridLayer.x=(_areaRec.width-_mapGridLayer.width)/2+_mapGridLayer.width/2;
			_dragRecLayer.x=(_areaRec.width-_mapGridLayer.width)/2+_mapGridLayer.width/2;
			drawEditArea();
		}
		
		private function drawEditArea():void
		{
			graphics.clear();
			graphics.beginFill(0xffffff);
			graphics.drawRect(0,0,_areaRec.width, _areaRec.height);
			graphics.endFill();
		}
		
		public function dispose():void
		{
			removeEvent();
			clearTerrainTileList();
			_terrainTileList=null;
			_terrainTileDic=null;
			_selectTerrainTileList=null;
			_areaRec=null;
			_overMapTerrainTileView=null;
			if(_mapGridLayer)
			{
				_mapGridLayer.dispose();
				_mapGridLayer=null;
			}
			if(parent)parent.removeChild(this);
		}
	}
}